package com.example.administrator.myapplication;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.tencent.bugly.beta.Beta;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView mTv;
    private Button mBtn1, mBtn2, mBtn3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTv = findViewById(R.id.tv);
        mBtn1 = findViewById(R.id.btn_1);
        mBtn2 = findViewById(R.id.btn_2);
        mBtn3 = findViewById(R.id.btn_3);
        mTv.setText("当前版本：" + getVersionName());
        mBtn1.setOnClickListener(this);
        mBtn2.setOnClickListener(this);
        mBtn3.setOnClickListener(this);
        mBtn2.setVisibility(View.GONE);
    }

    private String getVersionName() {
        // 获取packagemanager的实例
        PackageManager packageManager = getPackageManager();
        // getPackageName()是你当前类的包名，0代表是获取版本信息
        PackageInfo packInfo = null;
        try {
            packInfo = packageManager.getPackageInfo(getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        String version = packInfo.versionName;
        return version + BuildConfig.LOG_DEBUG;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_1:
                //%d这里是个bug,打包基准包
//                Toast.makeText(MainActivity.this, String.format("当前版本号：%d", getVersionName()), Toast.LENGTH_SHORT).show();
                //%s解决bug,打包补丁包
                Toast.makeText(MainActivity.this, String.format("当前版本号：%s", getVersionName()), Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_2:
                break;
            case R.id.btn_3:
                Beta.checkUpgrade(true, false);
                break;
            default:
                break;
        }
    }
}
